package com.txsj.scan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.txsj.scan.R;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.base.IView;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.bean.WarehouseBean;
import com.txsj.scan.fragment.AllGoodsFragment;
import com.txsj.scan.fragment.HistoryGoodsFragment;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by wangchuangjun on 2018/11/26
 * Desc: 商品分类列表页
 */

public class GoodsListActivity extends BaseActivity<BasePresenter> implements IView {

    public static final int REQUEST_SEARCH_CODE = 0x021;

    TextView tv_current_warehouse,tv_other_goods,tv_back;
    AllGoodsFragment currentGoodsFragment;
    HistoryGoodsFragment otherGoodsFragment;
    List<Fragment> fragmentList;
    WarehouseBean warehouseBean;
    ImageView iv_search_goods;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_list);
        initView();
        initData();
    }

    private void initData() {
        String json = getIntent().getStringExtra("data");
        warehouseBean = new Gson().fromJson(json,WarehouseBean.class);
        fragmentList = new ArrayList<>();

        currentGoodsFragment = new AllGoodsFragment();
        Bundle bundle1 = new Bundle();
        bundle1.putString("type","1");
        bundle1.putString("warehouse",new Gson().toJson(warehouseBean));
        currentGoodsFragment.setArguments(bundle1);

        otherGoodsFragment = new HistoryGoodsFragment();

        fragmentList.add(currentGoodsFragment);
        fragmentList.add(otherGoodsFragment);

        tableFragment(0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null == data || null == data.getStringExtra("data"))return;
        if (requestCode == REQUEST_SEARCH_CODE){
            GoodsInfo goodsInfo = new Gson().fromJson(data.getStringExtra("data"),GoodsInfo.class);
            Intent intent = getIntent();
            intent.putExtra("data", new Gson().toJson(goodsInfo));
            setResult(LabelActivity.RESULT_GOODS_CODE,intent);
            finish();
        }
    }

    private void initView() {
        tv_current_warehouse = (TextView) findViewById(R.id.tv_current_warehouse);
        tv_other_goods = (TextView) findViewById(R.id.tv_other_goods);
        tv_back = (TextView) findViewById(R.id.tv_back);
        iv_search_goods = (ImageView) findViewById(R.id.iv_search_goods);
        iv_search_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GoodsListActivity.this, SearchGoodsListActivity.class);
                startActivityForResult(intent,REQUEST_SEARCH_CODE);
            }
        });

//        CustomDialog dialog = new CustomDialog(this);
//        dialog.setTitleStr("温馨提示");
//        dialog.setContent("您可按下物理按钮扫描条码进行商品选择！");
//        dialog.setSimpleOkVisiable();
//        dialog.show();
    }

    public void tableFragment(int fragment_index) {

        if (fragment_index ==0){
            tv_current_warehouse.setBackground(getResources().getDrawable(R.drawable.shape_blue_left_select));
            tv_current_warehouse.setTextColor(getResources().getColor(R.color.theme_color));
            tv_other_goods.setBackground(getResources().getDrawable(R.drawable.shape_blue_right_no_select));
            tv_other_goods.setTextColor(getResources().getColor(R.color.white));
        }else {
            tv_current_warehouse.setBackground(getResources().getDrawable(R.drawable.shape_blue_left_no_select));
            tv_current_warehouse.setTextColor(getResources().getColor(R.color.white));
            tv_other_goods.setBackground(getResources().getDrawable(R.drawable.shape_blue_right_select));
            tv_other_goods.setTextColor(getResources().getColor(R.color.theme_color));
        }

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, fragmentList.get(fragment_index));
        transaction.commit();

    }

    @Override
    protected BasePresenter providePresenter() {
        return presenter = new BasePresenter(this) {
            @Override
            protected void initModel() {

            }
        };
    }

    @Override
    public Context context() {
        return this;
    }

    public void clickCurrent(View view) {
        tableFragment(0);
    }

    public void clickOther(View view) {
        tableFragment(1);
    }

    public void clickBack(View view) {
        finish();
    }
}
